//
//  ViewController.swift
//  AirlineReservation
//
//  Created by Kevin on 2019/12/26.
//  Copyright © 2019 Kevin. All rights reserved.
//
import UIKit

class VCMain: UITabBarController {
    
    // 客服按钮
    private lazy var serviceBtn = UIButton(iconName: "tabbar_compose_icon_add", backgroundIconName: "tabbar_compose_button")
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        init_views()
        init_customer_service()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        // 按钮放在前面
        tabBar.bringSubviewToFront(serviceBtn)
    }
    
    // 提示窗
    @objc func clickServiceBtn() -> Void {
        let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
        // 智能客服
        alert.addAction(UIAlertAction(title: "智能客服", style: .default, handler: { (UIAlertAction) in
            let vc = UINavigationController(rootViewController: VCAutoReply())
            vc.modalPresentationStyle = .fullScreen
            self.present(vc, animated: true, completion: nil)
        }))
        // 拨打电话
        alert.addAction(UIAlertAction(title: "拨打电话：8888-8888", style: .default, handler: { (UIAlertAction) in
            self.callPhone()
        }))
        alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
        present(alert, animated: true, completion: nil)
    }
    
    // 拨打电话
    private func callPhone() -> Void {
        guard let url = URL(string: "telprompt://88888888") else {
            return
        }
        if UIApplication.shared.canOpenURL(url) {
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
        }
    }
    
}


// MARK: - 初始化
extension VCMain {
    
    func init_views() -> Void {
        addChild(vc: VCHome(), title: "首页", imgName: "ic_action_home")
        addChild(vc: VCPlane(), title: "航班动态", imgName: "ic_action_plane")
        addChild(UIViewController())
        addChild(vc: VCJourney(), title: "行程", imgName: "ic_action_journey")
        addChild(vc: VCMineSetting(), title: "我的", imgName: "ic_action_mine")
    }
    
    func init_customer_service() -> Void {
        tabBar.addSubview(serviceBtn)
        tabBar.backgroundColor = Palette.colorFromRGB(66, green: 65, blue: 65)
        // vc的数量
        guard let cout = viewControllers?.count else {
            return
        }
        // 放置到中间
        let width = tabBar.bounds.width / CGFloat(cout) - 1
        serviceBtn.frame = tabBar.bounds.insetBy(dx: 2 * width, dy: 0)
        // 添加点击事件
        serviceBtn.addTarget(self, action: #selector(clickServiceBtn), for: .touchUpInside)
    }
    
    private func addChild(vc:UIViewController,title:String,imgName:String){
        // 标题
        vc.title = title
        // 图标
        vc.tabBarItem.image = UIImage(named: imgName)
        // 导航器
        let nav = UINavigationController(rootViewController: vc)
        nav.navigationBar.titleTextAttributes = [.font:UIFont.systemFont(ofSize: 20),.foregroundColor:COLOR_NICE]
        addChild(nav)
    }
}
